package com.allways.dubbo.mdm.service.logiarealine.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import com.allways.dubbo.mdm.beans.logiarealine.Logiarealine;
import com.allways.dubbo.mdm.service.logiarealine.ILogiarealineService;

/**
 * <p>ClassName: ILogiarealineService</p>
 * <p>Description: 物流运作区域明细Service接口</p>
 * <p>Author: weih</p>
 * <p>Date: 2016-10-14</p>
 */
@Service("mdmLogiarealineService")
public class LogiarealineServiceImpl extends MdmBaseDaoImpl implements ILogiarealineService {
	Log log = LogFactory.getLog(LogiarealineServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
    @Override
    public int addLogiarealine(Logiarealine logiarealine) {
   		log.debug("For the new logiarealine parameters:"+ logiarealine.toJson());
   		validColumnLen(logiarealine);
    	
        //无code的不能插入
        if(StringUtils.isEmpty(logiarealine.getLogiarealineCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != getLogiarealineByCode(logiarealine.getLogiarealineCode())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        int cnt=insert("logiarealine.insertLogiarealine",logiarealine);
        
        /*数据变更日志*/
        DataChangeLog log = new DataChangeLog();
        log.setDataType(DataChangeLog.DataType.logiarealine.getName());
        log.setDataPk(logiarealine.getLogiarealineId().toString());
        log.setModifyType(DataChangeLog.OPER_TYPE_ADD);
        dataChangeLogService.addDataChangeLog(log);
        
        return cnt;
    }

    @Override
    public int updateLogiarealine(Logiarealine logiarealine) {
    	log.debug("Update for the logiarealine parameters:"+ logiarealine.toJson());
    	validColumnLen(logiarealine);
     	//无code的不能更新
        if(StringUtils.isEmpty(logiarealine.getLogiarealineCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        Logiarealine oldLogiarealine = getLogiarealineByCode(logiarealine.getLogiarealineCode());
        if(null == oldLogiarealine){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
		oldLogiarealine.setId(logiarealine.getId());
		oldLogiarealine.setLogisticsAreaId(logiarealine.getLogisticsAreaId());
		oldLogiarealine.setLogisticsAreaCode(logiarealine.getLogisticsAreaCode());
		oldLogiarealine.setLogisticsAreaLineLevel(logiarealine.getLogisticsAreaLineLevel());
		oldLogiarealine.setLogisticsPointId(logiarealine.getLogisticsPointId());
		oldLogiarealine.setLogisticsPointCode(logiarealine.getLogisticsPointCode());
		oldLogiarealine.setCountryCode(logiarealine.getCountryCode());
		oldLogiarealine.setProvinceCode(logiarealine.getProvinceCode());
		oldLogiarealine.setCityCode(logiarealine.getCityCode());
		oldLogiarealine.setCountyCode(logiarealine.getCountyCode());
		oldLogiarealine.setIsDisabled(logiarealine.getIsDisabled());
		oldLogiarealine.setDataSource(logiarealine.getDataSource());
		oldLogiarealine.setCreatedBy(logiarealine.getCreatedBy());
		oldLogiarealine.setCreatedTime(logiarealine.getCreatedTime());
		oldLogiarealine.setUpdatedBy(logiarealine.getUpdatedBy());
		oldLogiarealine.setUpdatedTime(logiarealine.getUpdatedTime());
        
        int cnt= update("logiarealine.updateLogiarealine",oldLogiarealine);
        /*数据变更日志*/
        DataChangeLog log = new DataChangeLog();
        log.setDataType(DataChangeLog.DataType.Logiarealine.getName());
        log.setDataPk(oldLogiarealine.getLogiarealineId().toString());
        log.setModifyType(DataChangeLog.OPER_TYPE_EDIT);
        dataChangeLogService.addDataChangeLog(log);
        
        return cnt;
    }

    @Override
    public int deleteLogiarealine(Logiarealine logiarealine) {
    	log.debug("Delete for the logiarealine parameters:"+ logiarealine.toJson());
    	 
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(logiarealine.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
     	//无code的不能删除
        if(StringUtils.isEmpty(logiarealine.getLogiarealineCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        Logiarealine fullLogiarealine = getLogiarealineByCode(logiarealine.getLogiarealineCode());
        
        /* 数据变更日志 */
        DataChangeLog log = new DataChangeLog();
        log.setDataType(DataChangeLog.DataType.Logiarealine.getName());
        log.setDataPk(fullLogiarealine.getLogiarealineId().toString());
        log.setModifyType(DataChangeLog.OPER_TYPE_DELETE);
        dataChangeLogService.addDataChangeLog(log);
        
        return delete("logiarealine.deleteLogiarealine",logiarealine);
    }

    @Override
    public Logiarealine getLogiarealineByCode(String code) {
    	log.debug("Query for logiarealine code:" + code);
        return queryForObject("logiarealine.getLogiarealineByCode",code);
    }
    @Override
    public long getLogiarealineCount(Logiarealine logiarealine) {
    	log.debug("Query for logiarealine count parameters:" + logiarealine.toJson());
        return queryForObject("logiarealine.getLogiarealineCount",logiarealine);
    }
   @Override
    public List<Logiarealine> queryLogiarealines(Logiarealine logiarealine, int firstRow, int lastRow) {
      	log.debug("Query for logiarealine parameters:" + logiarealine.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("logiarealine",logiarealine);
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("logiarealine.queryLogiarealines", param);
    }
    
    /**
     * 验证字段长度
     * @param logiarealine 需要验证的对象
     */
    private void validColumnLen(Logiarealine logiarealine){
		if(!StringUtils.isEmpty(logiarealine.getId()) && logiarealine.getId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiarealine.getLogisticsAreaId()) && logiarealine.getLogisticsAreaId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiarealine.getLogisticsAreaCode()) && logiarealine.getLogisticsAreaCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(logiarealine.getLogisticsAreaLineLevel()) && logiarealine.getLogisticsAreaLineLevel().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiarealine.getLogisticsPointId()) && logiarealine.getLogisticsPointId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(logiarealine.getLogisticsPointCode()) && logiarealine.getLogisticsPointCode().length()>40){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{40},null);
        }
		if(!StringUtils.isEmpty(logiarealine.getCountryCode()) && logiarealine.getCountryCode().length()>2){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{2},null);
        }
		if(!StringUtils.isEmpty(logiarealine.getProvinceCode()) && logiarealine.getProvinceCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(logiarealine.getCityCode()) && logiarealine.getCityCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
		if(!StringUtils.isEmpty(logiarealine.getCountyCode()) && logiarealine.getCountyCode().length()>6){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{6},null);
        }
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(logiarealine.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
